package com.pharmacy.controller;


import com.pharmacy.dto.MedicinesAddDto;
import com.pharmacy.dto.MedicinesPageSelectDto;
import com.pharmacy.dto.MedicinesUpdateDto;
import com.pharmacy.dto.ReduceMedicineDto;
import com.pharmacy.entity.Medicines;
import com.pharmacy.result.PageResult;
import com.pharmacy.result.Result;
import com.pharmacy.service.MedicinesService;
import com.pharmacy.vo.MedicinesVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/medicines")
@Slf4j
@Tag(name = "药品管理")
@CrossOrigin
public class MedicinesController {

    @Autowired
    private MedicinesService medicinesService;

    @Operation(summary = "添加药品")
    @PostMapping("/medicinesAdd")
    public Result<String> addMedinines(@RequestBody MedicinesAddDto medicinesAddDto){

        medicinesService.addMedinines(medicinesAddDto);

        return Result.success("添加成功");
    }

    /**
     * 根据条形码查询药品
     * @param barcode
     * @return
     */
    @Operation(summary = "查询药品")
    @GetMapping("/barcodeSelect")
    public Result<MedicinesVo> getMedicines(String barcode){

        MedicinesVo medicines = medicinesService.getMedicines(barcode);

        return Result.success(medicines);
    }

    /**
     * 药品分页查询
     * @return
     */
    @Operation(summary = "药品分页查询")
    @GetMapping("/allSelectMedicines")
    public Result<PageResult> allSelectMedicines(MedicinesPageSelectDto medicinesPageSelectDto){
        log.info("药品分页查询:{}",medicinesPageSelectDto);
        PageResult pageResult =medicinesService.medicinesPageSelect(medicinesPageSelectDto);
        return Result.success(pageResult);
    }

    @Operation(summary = "根据id查询药品")
    @GetMapping("/{id}")
    public Result<Medicines> getById(@PathVariable Integer id) {
        log.info("根据id查询药品:{}",id);
        Medicines medicines = medicinesService.getById(id);
        return Result.success(medicines);
    }

    /**
     * 删除药品
     * @param id
     * @return
     */
    @Operation(summary = "删除药品")
    @DeleteMapping("/deleteMedicines/{id}")
    public Result<String> deleteMedicines(@PathVariable Integer id){
        medicinesService.deleteMedicines(id);
        return Result.success("删除成功");
    }

    /**
     * 修改药品信息
     * @param medicinesUpdateDto
     * @return
     */
    @Operation(summary = "修改药品信息")
    @PutMapping("/updateMedicines")
    public Result<String> updateMedicines(@RequestBody MedicinesUpdateDto medicinesUpdateDto){
        log.info("修改药品信息{}",medicinesUpdateDto);
        medicinesService.updateMedicines(medicinesUpdateDto);
        return Result.success("修改成功");
    }

    /**
     * 减少药品数量
     * @param reduceMedicineDto
     * @return
     */
    @Operation(summary = "减少药品数量")
    @PutMapping("/reduceMedicine")
    public Result<String> reduceMedicine(@RequestBody ReduceMedicineDto reduceMedicineDto){
        log.info("减少药品库存:{}",reduceMedicineDto);
        medicinesService.reduceMedicine(reduceMedicineDto);
        return Result.success();
    }
}
